package com.shujia.kafka;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.state.*;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

import java.awt.image.ImageProducer;
import java.security.Key;
import java.util.HashMap;

public class GlobeSort {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//            env.enableCheckpointing(5000);
//            env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
//            env.setStateBackend(new HashMapStateBackend());
//            env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/flink/checkpoint");

        //配置数据源
//            KafkaSource<String> src = KafkaSource.<String>builder()
//                    .setBootstrapServers("hadoop102:9092,hadoop103:9092,hadoop104:9092")
//                    .setTopics("words")
//                    .setGroupId("GlobeSort")
//                    .setValueOnlyDeserializer(new SimpleStringSchema())
//                    .build();
//            DataStreamSource<String> wordDS = env.fromSource(src, WatermarkStrategy.noWatermarks(), "word");

        DataStreamSource<String> wordDS = env.socketTextStream("hadoop102", 8888);
        env.setParallelism(1);
        SingleOutputStreamOperator<String> wordsDs = wordDS.flatMap((line, out) -> {
            for (String s : line.split(",")) {
                out.collect(s);
            }
        }, Types.STRING);

        DataStream<Tuple2<String, Integer>> keyBy = wordsDs.map(word -> new Tuple2<String, Integer>(word, 1), Types.TUPLE(Types.STRING, Types.INT));


        KeyedStream<Tuple2<String, Integer>, Object> wordList = keyBy.keyBy(kv -> kv.f0);
        SingleOutputStreamOperator<Tuple2<String, Integer>> list = wordList
                .process(new ProcessFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() {
                    ListState<Tuple2<String, Integer>> wordList;
                    ValueState<HashMap<String, Integer>> words;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        RuntimeContext context = getRuntimeContext();
                        ListStateDescriptor<Tuple2<String, Integer>> des = new ListStateDescriptor<Tuple2<String, Integer>>("wordList", TypeInformation.of(new TypeHint<Tuple2<String, Integer>>() {
                        }));
                        ValueStateDescriptor<HashMap<String, Integer>> words1 = new ValueStateDescriptor<>("words", TypeInformation.of(new TypeHint<HashMap<String, Integer>>() {
                        }));
                        wordList = context.getListState(des);

                        words1.setQueryable("words");
                        words = context.getState(words1);
                    }

                    @Override
                    public void processElement(Tuple2<String, Integer> stringIntegerTuple2, ProcessFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>.Context context, Collector<Tuple2<String, Integer>> collector) throws Exception {
                        String key = stringIntegerTuple2.f0;
                        if (words.value() == null) {
                            words.update(new HashMap<String, Integer>());
                        }
                        Integer value = words.value().getOrDefault(key, 0);
                        value++;
                        words.value().put(key, value);
                        for (String k : words.value().keySet()) {
                            collector.collect(Tuple2.of(k, words.value().get(k)));
                        }

                    }
                }, Types.TUPLE(Types.STRING, Types.INT));
        list.print();

        env.execute();

    }
}